package com.jwthhealth_pub.utils;

import android.util.Log;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;

/**
 * Created by liuzhen000 on 2016/7/18.
 */
public class TimeUtil {
    public static String format(int time) {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("mm:ss");
        Date date = new Date(time * 1000);
        return simpleDateFormat.format(date);
    }

    /**
     * 转换秒数成时间字符串
     *
     * @param second 秒数 1000
     * @return 如'20:12'
     */
    public static String getString(int second) {
        StringBuffer str = new StringBuffer();
        StringBuffer hstring = new StringBuffer();
        StringBuffer mstring = new StringBuffer();
        StringBuffer sstring = new StringBuffer();
        int hour = 0;
        int minute = 0;
        if (second > 60) {
            minute = second / 60;
            second = second % 60;
        }
        if (minute > 60) {
            hour = minute / 60;
            minute = minute % 60;
        }
        if (hour < 10) {
            hstring.append("0").append(String.valueOf(hour));
        } else {
            hstring.append(String.valueOf(hour));
        }
        if (minute < 10) {
            mstring.append("0").append(String.valueOf(minute));
        } else {
            mstring.append(String.valueOf(minute));
        }
        if (second < 10) {
            sstring.append("0").append(String.valueOf(second));
        } else {
            sstring.append(String.valueOf(second));
        }
        if ("00".equals(hstring.toString())) {
            str.append(mstring).append(":").append(sstring);
        } else {
            str.append(hstring).append(":").append(mstring).append(":")
                    .append(sstring);
        }

        return str.toString();
    }

    /**
     * 转换时间字符串成秒数
     *
     * @param time 如'20:10:12'
     * @return second 秒
     */
    public static int getSecond(String time) {
        String[] times = time.split(":");
        int hour = 0;
        int minute = 0;
        int second = 0;
        if (times.length == 2) {
            minute = Integer.parseInt(times[0]);
            second = Integer.parseInt(times[1]);
        } else {
            hour = Integer.parseInt(times[0]);
            minute = Integer.parseInt(times[1]);
            second = Integer.parseInt(times[2]);
        }
        return hour * 3600 + minute * 60 + second;
    }

    /**
     * 转换时间字符串成毫秒数
     *
     * @param time 如'20:10.12'
     * @return msecond 毫秒
     */
    public static int getMillSecond(String time) {
        String[] times = time.split(":");
        int minute;
        int second;
        int count = times.length;
        if (count < 2) {
            return 0;
        }
        minute = Integer.parseInt(times[0]);
        second = (int) (Float.parseFloat(times[1]) * 1000);
        return minute * 60 * 1000 + second;
    }

    /**
     * 转换秒数成分钟时间字符串
     *
     * @param second 秒数 1000
     * @return 如'80'
     */
    public static int getMinute(int second) {
        int hour = 0;
        int minute = 0;
        if (second > 60) {
            minute = second / 60;
            second = second % 60;
        } else {
            return 1;
        }
        if (minute > 60) {
            hour = minute / 60;
            minute = minute % 60;
        }
        return hour * 60 + minute;
    }

    /**
     * 转换秒数成小时,分钟时间字符串
     *
     * @param second 秒数 1000
     * @return 如'80'
     */
    public static String getHour(int second) {
        int hour = 0;
        int minute = 0;
        if (second > 60) {
            minute = second / 60;
            second = second % 60;
        } else {
            return "0分钟";
        }
        if (minute > 60) {
            hour = minute / 60;
            minute = minute % 60;
            return hour + "小时";
        } else {
            return minute + "分钟";
        }
    }

    /**
     * 将秒转换为几分几秒
     *
     * @param second
     * @return
     */
    public static String formatSecond(int second) {
        int minutes = (second % (60 * 60)) / 60;
        int seconds = second % (60);
        return minutes + " '" + seconds + " '' ";
    }

    public static String getNowDate(){
        SimpleDateFormat formatter = new SimpleDateFormat ("yyyy-MM-dd   HH:mm:ss     ");
        Date curDate = new Date(System.currentTimeMillis());//获取当前时间
        String Standby = formatter.format(curDate);
        return  Standby;
    }
    public static String getNowDateShort(){
        SimpleDateFormat formatter = new SimpleDateFormat ("yyyy-MM-dd");
        Date curDate = new Date(System.currentTimeMillis());//获取当前时间
        String Standby = formatter.format(curDate);
        return  Standby;
    }

    /**
     * 获取过去或者未来 任意天内的日期数组
     * @param intervals      intervals天内
     * @return              日期数组
     */
    public static ArrayList<String> test(int intervals ) {
        ArrayList<String> pastDaysList = new ArrayList<>();
        ArrayList<String> fetureDaysList = new ArrayList<>();
        for (int i = 0; i <intervals; i++) {
            pastDaysList.add(getPastDate(i));
            fetureDaysList.add(getFetureDate(i));
        }
        return pastDaysList;
    }

    /**
     * 获取过去第几天的日期
     *
     * @param past
     * @return
     */
    public static String getPastDate(int past) {
        Calendar calendar = Calendar.getInstance();
        calendar.set(Calendar.DAY_OF_YEAR, calendar.get(Calendar.DAY_OF_YEAR) - past);
        Date today = calendar.getTime();
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        String result = format.format(today);
        Log.e(null, result);
        return result;
    }


    /**
     * 获取未来 第 past 天的日期
     * @param past
     * @return
     */
    public static String getFetureDate(int past) {
        Calendar calendar = Calendar.getInstance();
        calendar.set(Calendar.DAY_OF_YEAR, calendar.get(Calendar.DAY_OF_YEAR) + past);
        Date today = calendar.getTime();
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        String result = format.format(today);
        Log.e(null, result);
        return result;
    }




    /**
     * 将秒转换为几分几秒
     *
     * @param
     * @return
     */


    public static String minute2hourmin(int _mintue){
        int hours = _mintue / 60;
        int minute = _mintue % 60;

        return hours+"时" + minute+"分";
    }

    public static String second2hourmin(int second){
        int _minute = second / 60;
        int hours = _minute / 60;
        int minute = _minute % 60;

        return hours+"时" + minute+"分";
    }


    /**
     * 几时几分转换为分
     *
     * @param
     * @return
     */


    public static int getOfMinute(String _mintue){
        _mintue = _mintue.replace("时","-").replace("分","-");
        String [] arrayTemp = _mintue.split("-");
        int temp = Integer.valueOf(arrayTemp[0])*60+Integer.valueOf(arrayTemp[1]);
        return temp;
    }


}
